package com.one.common.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.one.common.R;


/**
 * Created by yy on 2017/3/31.
 * Toast
 */
public class MyToaster {

    public TextView title;
    private ImageView ivToast;
    public Toast mToast;// 如果定义成静态的变量，会造成内存泄露。

    public MyToaster(Context context) {
        mToast = new Toast(context.getApplicationContext());

        View layout = LayoutInflater.from(context).inflate(R.layout.layout_supertoast, null);
        title = (TextView) layout.findViewById(R.id.tv_message);
        ivToast = (ImageView) layout.findViewById(R.id.iv_toast);
        ivToast.setVisibility(View.GONE);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
    }

    /**
     * @param message Toast的信息
     */
    public void showToast(CharSequence message, int duration) {
            if (null != mToast) {
            mToast.setDuration(duration);
            title.setText(message);
            mToast.show();
        }
    }

    public void destroy() {
        mToast = null;
    }

    /**
     * 设置显示类型
     */
    public void setType(int type) {
        ivToast.setVisibility(View.VISIBLE);
        switch (type) {
            case SUCCESS:
                ivToast.setImageResource(R.mipmap.toast_success);
                break;
            case FAIL:
                ivToast.setImageResource(R.mipmap.toast_fail);
                break;
//            case INFO:
////                ivToast.setImageResource(R.drawable.icon_toast_info);
//                break;
//            case CRY:
////                ivToast.setImageResource(R.drawable.icon_toast_cry);
//                break;
            default:
                ivToast.setVisibility(View.GONE);
                break;

        }
    }

    /**
     * 成功
     */
    public static final int SUCCESS = 1;
    /**
     * 失败
     */
    public static final int FAIL = 2;
}
